{extend name="platform/base" /}
{block name="main"}
<section class="panel panel-default">
	<header class="panel-heading font-bold"> 修改品牌 </header>
	<div class="panel-body">
		<form class="form-horizontal" id="goods_brand_form">
			<div class="form-group">
				<label class="col-sm-2 control-label"><span class="color-red">*</span>品牌名称</label>
				<div class="col-md-2">
					<input type="text" class="form-control" id="brand_name" name="brand_name" value="{$brand_info['brand_name']}" required onkeydown="error_hide()">
				</div>
				<p class="error">请输入品牌名称</p>
			</div>
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label"><span class="color-red">*</span>品牌首字母</label>
				<div class="col-md-1">
					<input type="text" class="form-control" id="brand_initial" name="brand_initial" value="{$brand_info['brand_initial']}" required maxlength="1" onkeydown="error_hide()">
				</div>
				<p class="error">品牌首字母必须是单个字母</p>
			</div>
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label">是否推荐</label>
				<div class="col-md-4">
					<label class="switch">
						<input id="brand_recommend" type="checkbox" {if condition="$brand_info['brand_recommend'] eq 1"}checked{/if}>
						<span></span> 
					</label>
				</div>
			</div>

			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">品牌图片</label>
				<div class="col-md-2">
					<p class="imglogo-box">
						{if condition="$brand_info['brand_pic'] eq ''"}
						<img id="img_logo" class="imglogo img-size"src=""/>
						{else/}
						<img id="img_logo" class="imglogo img-size" src="__UPLOAD__/{$brand_info['brand_pic']}"/>
						{/if}
					</p>
					 <a href="javascript:void(0);" style="display:block;position:relative;z-index:1;">
							<span style="width: 97px;height: 34px;position: absolute;left: 0;top: 0;z-index: 2;cursor: pointer;"> 
								<input hidefocus="true" size="1" class="input-file" name="file_upload" id="logo" nc_type="change_store_label" type="file" style="width: 97px;height: 34px;padding: 0;margin: 0;border: none 0;opacity: 0;filter: alpha(opacity = 0);cursor: pointer;"onchange="imgUpload(this);"> 
								<input type="hidden" id="logo_value" value="{$brand_info['brand_pic']}" />
							</span>
							<a class="btn btn-default"><i class="fa fa-cloud-upload text"></i><span class="text">上传图片</span></a>
					</a>
				</div>
			</div>
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label">品牌推荐广告图</label>
				<div class="col-md-2">
					<p class="imglogo-box">
						{if condition="$brand_info['brand_ads'] eq ''"}
						<img id="img_adv" class="imglogo img-size" src=""/>
						{else/}
						<img id="img_adv" class="imglogo img-size" src="__UPLOAD__/{$brand_info['brand_ads']}"/>
						{/if}
					</p>
					<a href="javascript:void(0);" style="display:block;position:relative;z-index:1;">
						<span style="width: 97px;height: 34px;position: absolute;left: 0;top: 0;z-index: 2;cursor: pointer;"> 
							<input hidefocus="true" size="1" class="input-file" name="file_upload" id="adv" nc_type="change_store_label" type="file" style="width: 97px;height: 34px;padding: 0;margin: 0;border: none 0;opacity: 0;filter: alpha(opacity = 0);cursor: pointer;"onchange="imgUpload(this);"> 
							<input type="hidden" id="adv_value" value="{$brand_info['brand_ads']}" />
						</span>
						<a class="btn btn-default"><i class="fa fa-cloud-upload text"></i><span class="text">上传图片</span></a>
					</a>
				</div>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<div class="col-sm-4 col-sm-offset-2">
					<input type="hidden" id="brand_id" value="{$brand_info['brand_id']}">
					<!-- <button type="submit" class="btn btn-primary">保存</button> -->
					<a href="javascript:void(0);" onclick="updateGoodsBrandAjax()" class="btn btn-primary">保存</a>
				</div>
			</div>
		</form>
	</div>
</section>
<script>
$().ready(function() {
	$("#goods_brand_form").validate({
		submitHandler:function(){
			updateGoodsBrandAjax();
		}
	});
});

function validation() {
	var brand_name = $("#brand_name");
	var brand_initial = $("#brand_initial");
	var brand_pic = $("#brand_pic");
	var brand_ads = $("#brand_ads");
	if (brand_name.val() == "") {
		brand_name.focus().parent().next().show();
		return false;
	} else {
		brand_name.parent().next().hide();
	}
	
	if (brand_initial.val() == "") {
		brand_initial.focus().parent().next().show();
		return false;
	} else if (brand_initial.val().length != 1) {
		brand_initial.focus().parent().next("span").text("品牌首字母必须是单个字母").show();
		return false;
	} else {
		brand_initial.parent().next().hide();
	}
	return true;
}


var flag = false;//防止重复提交
function updateGoodsBrandAjax(){
	if (!validation()) {
		return;
	}
	var brand_id = $("#brand_id").val();
	var brand_name = $("#brand_name").val();
	var brand_initial = $("#brand_initial").val();
	if($("#brand_recommend").prop("checked")){
		var brand_recommend = 1;
	}else{
		var brand_recommend = 0;
	}

	var brand_pic = $("#logo_value").val();
	var brand_ads = $("#adv_value").val();
	$.ajax({
		type : "post",
		url : "{:__URL('PLATFORM_MAIN/goods/updategoodsbrand')}",
		data : {
			'brand_id':brand_id,
			'brand_name':brand_name,
			'brand_initial':brand_initial,
			'brand_recommend':brand_recommend,
			'brand_pic':brand_pic,
			'brand_ads':brand_ads
		},
		async : true,
		success : function (data) {
			if (data["code"] > 0) {
				showMessage('success', "商品品牌修改成功","{:__URL('PLATFORM_MAIN/goods/goodsbrandlist')}");
			}else{
				showMessage('error', "商品品牌修改失败");
				flag = false;
			}	
		}
	});
}

/**
 *图片上传
 */
function imgUpload(event) {
	var fileid = $(event).attr("id");
	var data = { 'file_path' : UPLOADCOMMON };
	uploadFile(fileid,data,function(res){
		if(res.code){
			$("#img_"+fileid).attr("src",__IMG(res.data));
			$("#"+fileid+"_value").val(res.data);
			showTip(res.message,"success");
		}else{
			showTip(res.message,"error");
		}
	});
}
</script>
{/block}